//
//  EditChecklistTextViewController.swift
//  ezyform
//
//  Created by MoennigQ on 12/3/19.
//  Copyright © 2019 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
import HandyJSON
import SwiftyJSON

class EditChecklistTextViewController: BaseViewController {
    
    let customView = PVDEditChecklistTextView()
    var serverModel: PVDChecklistModel!
    var oldAnswer: String = ""
    var saveClosure: ((PVDChecklistModel)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
    }
    func initUI(){
        self.view = self.customView
        self.customView.setTitleInfo(self.serverModel.title)
        
        self.customView.saveBtn.addTarget(self, action: #selector(saveBtnClick), for: UIControl.Event.touchUpInside)
        //
        self.customView.setData(serverModel)
        self.oldAnswer = serverModel.answer
    }
    
    @objc
    func saveBtnClick(){
        let editText = self.customView.msgTextView.textView.text.trimmingCharacters(in: .whitespaces)
        if editText == oldAnswer && serverModel.oper_type == "" {//没有修改，且原来也不是本地数据
            //不做任何操作
        }else{
            self.serverModel.answer = editText
            if editText == "" {
                self.serverModel.answered_at = ""
            }else{
                self.serverModel.answered_at = DateUtil().getCurrentDatetime()
            }
            self.saveClosure?(self.serverModel)
            //返回到上一级
        }
        self.backToPre()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
